<?php

namespace common\model;

use think\Hook;
use think\model\RelationModel;

class ReviewRequestNodeModel extends RelationModel
{

    //自动验证
    protected $_validate = [
        ['review_request_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],
        ['role_code', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],
        ['parent_id', '', '', self::EXISTS_VALIDATE, 'integer', self::MODEL_BOTH],//必须字段
    ];

    //自动完成
    protected $_auto = [
        ['uuid', 'create_uuid', self::MODEL_INSERT, 'function'],
        ['is_pass', 'fillDefaultNo', self::MODEL_INSERT, 'callback'],
        ['is_reviewed', 'fillDefaultNo', self::MODEL_INSERT, 'callback'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['param', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['real_reviewer', 'auto_fill_default_id', self::MODEL_INSERT, 'function_with_data'],

    ];

    /**
     * 填充no
     * @param $args
     * @return string
     */
    public function fillDefaultNo($args)
    {
        if (empty($args)) {
            return "no";
        }
        return $args;
    }

    /**
     * 参数
     * @param $value
     * @return array|mixed
     */
    public function getParamAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }

    /**
     * 重载 after insert
     * @param $pk
     * @param $pkName
     * @param $data
     * @param $options
     */
    public function _after_insert($pk, $pkName, $data, $options)
    {
        parent::_after_insert($pk, $pkName, $data, $options);
        if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){
            // 禁用db后处理事件
            return;
        }

        Hook::listen('after_review_request_node_created', $data);
    }

    /**
     * 更新触发
     * @param $result
     * @param $pkName
     * @param $data
     * @param $options
     * @param $writeEvent
     */
    public function _after_update($result, $pkName, $data, $options, $writeEvent)
    {
        parent::_after_update($result, $pkName, $data, $options, $writeEvent);
        if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){
            // 禁用db后处理事件
            return;
        }

        Hook::listen('after_review_request_node_updated', $data);
    }

}
